package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * @author MissChen
 * @date 2024年04月23日 8:43
 */
@WebServlet("/findAll")
public class EmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");
        out.println(" <head>");
        out.println(" <meta charset='UTF-8'>");
        out.println(" <title>员工列表</title>");
        out.println(" </head>");
        out.println(" <body>");
        out.println("      <h1>员工列表</h1>");
        out.println("     <table>");
        out.println("         <thead>");
        out.println("            <td>编号</td>");
        out.println("            <td>姓名</td>");
        out.println("            <td>工资</td>");
        out.println("            <td>操作</td>");
        out.println("       </thead>");
        out.println("      <tbody>");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "root");
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                out.println("          <tr>");
                out.println("             <td>" + rs.getInt("id") + "</td>");
                out.println("              <td>" + rs.getString("name") + "</td>");
                out.println("              <td>" + rs.getDouble("salary") + "</td>");
                out.println("               <td>");

                out.println("                   <a href='showModifyEmp?id="+rs.getInt("id")+"'>修改</a>");
                out.println("                 <a href='deleteEmp?id="+rs.getInt("id")+"'>删除</a>");
                out.println("               </td>");
                out.println("           </tr>");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


        out.println("       </tbody>");
        out.println("   </table>");
        out.println("</body>");
        out.println("</html>");

    }
}
